package com.swipemaster.activities;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;

import com.google.analytics.tracking.android.EasyTracker;
import com.purplebrain.adbuddiz.sdk.AdBuddiz;
import com.swipemaster.R;
import com.swipemaster.common.Global;
import com.swipemaster.dialogs.GameDialog;
import com.swipemaster.game.GameSurface;
import com.swipemaster.record.RecordsManager;

/** All games are running in this Activity. */
public class GameActivity extends Activity
{
	@Override
	public void onBackPressed()
	{
		Global.app.gameSurface.setRunning(false);
		Global.app.dialog.showOnGameStart();
	}

	public void onGameCreate()
	{
		Global.app.isDuringGameplay = false;
		Global.app.gameMode.playSong();
		Global.app.gameSurface = new GameSurface(this);
		setContentView(Global.app.gameSurface);
		Global.app.dialog = new GameDialog(this);
		Global.app.dialog.showOnGameStart();
	}

	public void onGameOver(final boolean isWinner)
	{
		Global.app.isDuringGameplay = false;
		RecordsManager.addGamesPlayed();
		Global.app.gameMode.addModePlayed(isWinner);
		Global.app.gameMode.onGameOver(this, isWinner);
		RecordsManager.saveRecords();
	}

	/** Starting point of a new game. */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		EasyTracker.getInstance().activityStart(this);

		if (Global.app.gameMode == null)
		{
			finish();
			return;
		}

		getWindow().setFormat(PixelFormat.RGBA_8888);
		Global.app.musicPlayer.continueMusic = true;
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
		onGameCreate();
	}

	@Override
	protected void onPause()
	{
		Global.app.musicPlayer.stopMusic();
		Global.app.gameSurface.setRunning(false);

		super.onPause();
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		AdBuddiz.getInstance().onStart(this);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onResume()
	{
		if (Global.app.musicPlayer.continueMusic)
		{
			Global.app.musicPlayer.continueMusic = false;
		}
		else
		{
			Global.app.gameMode.playSong();
		}

		if (!Global.app.dialog.isShowing() && !isFinishing())
		{
			Global.app.dialog.showOnGameStart();
		}

		super.onResume();
	}
}
